package cn.ctodb.mall.config.service.impl;

import cn.ctodb.mall.config.service.ConfigService;
import cn.ctodb.mall.config.domain.Config;
import cn.ctodb.mall.config.repository.ConfigRepository;
import cn.ctodb.mall.config.service.dto.ConfigDTO;
import cn.ctodb.mall.config.service.mapper.ConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Config.
 */
@Service
public class ConfigServiceImpl implements ConfigService{

    private final Logger log = LoggerFactory.getLogger(ConfigServiceImpl.class);

    private final ConfigRepository configRepository;

    private final ConfigMapper configMapper;

    public ConfigServiceImpl(ConfigRepository configRepository, ConfigMapper configMapper) {
        this.configRepository = configRepository;
        this.configMapper = configMapper;
    }

    /**
     * Save a config.
     *
     * @param configDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ConfigDTO save(ConfigDTO configDTO) {
        log.debug("Request to save Config : {}", configDTO);
        Config config = configMapper.configDTOToConfig(configDTO);
        config = configRepository.save(config);
        ConfigDTO result = configMapper.configToConfigDTO(config);
        return result;
    }

    /**
     *  Get all the configs.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    public Page<ConfigDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Configs");
        Page<Config> result = configRepository.findAll(pageable);
        return result.map(config -> configMapper.configToConfigDTO(config));
    }

    /**
     *  Get one config by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    public ConfigDTO findOne(String id) {
        log.debug("Request to get Config : {}", id);
        Config config = configRepository.findOne(id);
        ConfigDTO configDTO = configMapper.configToConfigDTO(config);
        return configDTO;
    }

    /**
     *  Delete the  config by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(String id) {
        log.debug("Request to delete Config : {}", id);
        configRepository.delete(id);
    }

    @Override
    public String getConf(String type, String code){
        return configRepository.findOneByTypeAndCode(type,code).getConfValue();
    }
}
